package com.kh.pajx.sop.view.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.HsEquSwitch;
import com.kh.pajx.sop.domain.HsEquipmentInfo;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

/**
 * 设备开启和关闭时间action
 * 
 * @author 熊刘伟
 * @createDate 2014年6月26日 下午6:01:17
 * @return
 * @param
 */
@Controller
@Scope("prototype")
public class EquipmentSwitchAction extends ModelDrivenBaseAction<HsEquSwitch> {

    private HsEquSwitch equSwitch;
    private String equNos;
    private String startTime;
    private String endTime;
    private String swhIds;

    // 跳转到新增页面
    public String addUI() {
	return "addUI";
    }

    // 新增时间段
    /** 添加 */
    public void add() {
	DwzAjaxResult result = new DwzAjaxResult();
	try {
	    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
	    if (StringUtils.isEmpty(startTime) || startTime.equals("")) {
		result.setStatusCode("300");
		result.setMessage("保存失败，开始时间不能为空!");
		result.setCallbackType("");
		result.setForwardUrl("");
		result.setConfirmMsg("");
		result.setNavTabId("");
		DwzAjaxResult.josnMessage(result);
	    } else {
		boolean flag = true;
		if (!endTime.equals("")) {
		    Date d1, d2;
		    try {
			d1 = df.parse(startTime);
			d2 = df.parse(endTime);
			if (d1.getTime() > d2.getTime()) {
			    flag = false;
			}
		    } catch (ParseException e) {
			result.setMessage("系统错误,添加失败");
			result.setStatusCode("300");
			DwzAjaxResult.josnMessage(result);
		    }

		}
		if (!flag) {
		    result.setStatusCode("300");
		    result.setMessage("保存失败，开始时间不能大于结束时间!");
		    result.setCallbackType("");
		    result.setForwardUrl("");
		    result.setConfirmMsg("");
		    result.setNavTabId("");
		    DwzAjaxResult.josnMessage(result);
		} else {
		    String userId = getCurrentUser().getUserId();
		    HttpServletRequest request = ServletActionContext.getRequest();
		    String remark = request.getParameter("remark");
		    hsEquSwitchService.save(equNos, startTime, endTime, userId, remark);
		    // 设置跳转
		    result.setStatusCode("200");
		    result.setMessage("添加成功!");
		    result.setCallbackType("");
		    result.setConfirmMsg("");
		    result.setNavTabId("equlist");
		    result.setCloseDialog("addSwitch");
		    DwzAjaxResult.josnMessage(result);
		}
	    }
	} catch (Exception e) {
	    result.setMessage("添加失败！" + e.getMessage());
	    result.setStatusCode("300");
	    DwzAjaxResult.josnMessage(result);
	}

    }

    // 查看设备开启时间关闭时间列表
    public String switchList() {
	Map request = (Map) ActionContext.getContext().get("request");
	String[] userIds;
	try {

	    // List<InsideUser> insideUserList =
	    // insideUserService.findUserList();
	    if (StringUtils.isNotEmpty(equNos)) {
		/*
		 * azzcsimp 修改 List<HsEquipmentInfo> equipments =
		 * hsEquSwitchService .getByIds(equNos.split(","));
		 */
		/** 此处不能使用getByIds，因为此处根据equNo不是主键，并且为long类型 */
		List<HsEquipmentInfo> equipments = hsEquipmentInfoService.getByNos(equNos.split(","));
		userIds = new String[equipments.size()];
		Map<Long, HsEquipmentInfo> eqs = new HashMap<Long, HsEquipmentInfo>();
		for (HsEquipmentInfo hsEquipmentInfo : equipments) {
		    userIds[equipments.indexOf(hsEquipmentInfo)] = hsEquipmentInfo.getCreateOperId();
		    eqs.put(hsEquipmentInfo.getEquNo(), hsEquipmentInfo);
		}
		request.put("eqs", eqs);
	    } else {
		List<HsEquipmentInfo> equipments = hsEquSwitchService.findByCon();
		userIds = new String[equipments.size()];
		Map<Long, HsEquipmentInfo> eqs = new HashMap<Long, HsEquipmentInfo>();
		for (HsEquipmentInfo hsEquipmentInfo : equipments) {
		    userIds[equipments.indexOf(hsEquipmentInfo)] = hsEquipmentInfo.getCreateOperId();
		    eqs.put(hsEquipmentInfo.getEquNo(), hsEquipmentInfo);
		}
		request.put("eqs", eqs);
	    }
	    if (userIds != null) {
		List<SysInsideUser> users = hsEquSwitchService.findUserById(userIds);
		Map<String, SysInsideUser> us = new HashMap<String, SysInsideUser>();
		for (SysInsideUser insideUser : users) {
		    us.put(insideUser.getIsuId(), insideUser);
		}
		request.put("users", us);
	    }
	    // 分页查询-------------------------------------------
	    QueryHelper query = new QueryHelper(HsEquSwitch.class, "h").addCondition("h.relationStatusFlag = ?", "1");
	    // 条件查询-----------------------------
	    if (StringUtils.isNotEmpty(equNos)) {
		String str = "(" + equNos.trim();
		str += ")";
		if (StringUtils.isNotEmpty(str) && !str.equals("")) {
		    query.addCondition("h.equNo in " + str, null);
		    query.addOrderProperty("h.equNo", true);
		}
	    }
	    // -----------------------------

	    if (equSwitch != null) {
		if (equSwitch.getEquNo() > 0) {
		    query.addCondition("h.equNo=?", equSwitch.getEquNo());
		    query.addOrderProperty("h.equNo", true);
		}
	    }

	    query.preparePageBean(hsEquSwitchService, pageNum, numPerPage);

	    // --------------------------------------------------
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return "list";
    }

    // 启用时间的废弃

    public void delete() throws Exception {
	int delNums = hsEquSwitchService.deleteByequNo(swhIds);
	DwzAjaxResult result = new DwzAjaxResult();
	try {
	    result.setStatusCode("200");
	    result.setMessage("废弃成功" + delNums + "条！");
	    result.setConfirmMsg("");
	    result.setNavTabId("showEquSwitch");
	    DwzAjaxResult.josnMessage(result);
	} catch (Exception e) {
	    result.setMessage("废弃失败");
	    result.setStatusCode("300");
	    DwzAjaxResult.josnMessage(result);
	}

    }

    public String getEquNos() {
	return equNos;
    }

    public void setEquNos(String equNos) {
	this.equNos = equNos;
    }

    public HsEquSwitch getEquSwitch() {
	return equSwitch;
    }

    public void setEquSwitch(HsEquSwitch equSwitch) {
	this.equSwitch = equSwitch;
    }

    public String getStartTime() {
	return startTime;
    }

    public void setStartTime(String startTime) {
	this.startTime = startTime;
    }

    public String getEndTime() {
	return endTime;
    }

    public void setEndTime(String endTime) {
	this.endTime = endTime;
    }

    public String getSwhIds() {
	return swhIds;
    }

    public void setSwhIds(String swhIds) {
	this.swhIds = swhIds;
    }

}
